﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Web.Routing;

namespace MvcControlToolkit
{
    public class JavaScriptRouteValueDictionaryConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(RouteValueDictionary).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType)
        {
            throw new NotImplementedException();
        }

        public override void WriteJson(JsonWriter writer, object value)
        {
            RouteValueDictionary dict = value as RouteValueDictionary;
            JsonSerializer serializer = new JsonSerializer();

            foreach (JsonConverter json in AjaxHelperExtensions.CommonConverters)
                serializer.Converters.Add(json);

            writer.WriteStartObject();            
            foreach(KeyValuePair<string, object> pair in dict) 
            {
                writer.WritePropertyName(pair.Key);
                serializer.Serialize(writer, pair.Value);
            }
            writer.WriteEndObject();
        }
    }
}
